package cn.schoolwow.workflow.module.definition.query.kit;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.workflow.domain.instance.ManualWorkFlowAssigner;
import cn.schoolwow.workflow.entity.WorkFlowDefinition;
import cn.schoolwow.workflow.module.common.domain.QuickWorkFlowConfig;
import cn.schoolwow.workflow.module.definition.query.service.GetManualWorkFlowAssignerListServiceFlow;
import cn.schoolwow.workflow.module.definition.query.service.GetWorkFlowDefinitionListServiceFlow;
import cn.schoolwow.workflow.module.definition.query.service.HasDeployWorkFlowServiceFlow;

import java.util.ArrayList;
import java.util.List;

public class WorkFlowDeployQueryImpl implements WorkFlowDeployQuery {
    private QuickWorkFlowConfig quickWorkFlowConfig;

    public WorkFlowDeployQueryImpl(QuickWorkFlowConfig quickWorkFlowConfig) {
        this.quickWorkFlowConfig = quickWorkFlowConfig;
    }

    @Override
    public List<WorkFlowDefinition> getWorkFlowDefinitionList(){
        FlowContext flowContext = quickWorkFlowConfig.workflow.startFlow(new GetWorkFlowDefinitionListServiceFlow())
                .execute();
        List<WorkFlowDefinition> workFlowDefinitionList = (List<WorkFlowDefinition>) flowContext.getData("workFlowDefinitionList", new ArrayList<>());
        return workFlowDefinitionList;
    }

    @Override
    public boolean hasDeploy(String name) {
        FlowContext flowContext = quickWorkFlowConfig.workflow.startFlow(new HasDeployWorkFlowServiceFlow())
                .putCurrentCompositeFlowData("name", name)
                .execute();
        boolean exist = flowContext.checkData("exist", boolean.class);
        return exist;
    }

    @Override
    public List<ManualWorkFlowAssigner> getManualWorkFlowAssignerList(String definitionName) {
        FlowContext flowContext = quickWorkFlowConfig.workflow.startFlow(new GetManualWorkFlowAssignerListServiceFlow())
                .putCurrentCompositeFlowData("definitionName", definitionName)
                .execute();
        List<ManualWorkFlowAssigner> manualWorkFlowAssignerList = flowContext.getData("manualWorkFlowAssignerList", List.class);
        return manualWorkFlowAssignerList;
    }


}
